package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcWatchDataHeartMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcWatchData;
import com.csun.cmny.provider.model.domain.DmcWatchDataHeart;
import com.csun.cmny.provider.model.dto.watch.WatchHeartQueryDto;
import com.csun.cmny.provider.model.vo.watch.DmcWatchDataVo;
import com.csun.cmny.provider.model.vo.watch.WatchHeartDayVo;
import com.csun.cmny.provider.model.vo.watch.WatchHeartRecordVo;
import com.csun.cmny.provider.service.DmcWatchDataHeartService;
import com.csun.cmny.provider.service.DmcWatchDataService;
import com.csun.cmny.util.PublicUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-24 15:08
 * @Description: the class dmc watch data heart service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcWatchDataHeartServiceImpl extends BaseService<DmcWatchDataHeart> implements DmcWatchDataHeartService {

    @Resource
    private DmcWatchDataService dmcWatchDataService;

    @Resource
    private DmcWatchDataHeartMapper dmcWatchDataHeartMapper;

    /**
     * query hist heart with day
     *
     * @param watchHeartQueryDto watch heart query dto
     * @return the watch heart day vo
     */
    @Override
    public WatchHeartDayVo queryHistHeartWithDay(WatchHeartQueryDto watchHeartQueryDto) {

        Integer heartRateMax = 0;
        Integer heartRateMin = 0;
        Integer heartRateAvg = 0;

        dmcWatchDataService.getWatchDataByDeviceId(watchHeartQueryDto.getDeviceId());

        WatchHeartDayVo watchHeartDayVo = new WatchHeartDayVo();

        List<WatchHeartRecordVo> watchHeartRecordVos = dmcWatchDataHeartMapper
                .queryHistHeartWithDay(watchHeartQueryDto);

        if (PublicUtil.isEmpty(watchHeartRecordVos)) {
            return null;
        } else {
            // 最大值
            heartRateMax = watchHeartRecordVos.stream()
                    .mapToInt(WatchHeartRecordVo::getHeartRate).max().getAsInt();
            // 最小值
            heartRateMin = watchHeartRecordVos.stream()
                    .mapToInt(WatchHeartRecordVo::getHeartRate).min().getAsInt();
            // 平均值
            heartRateAvg = (int) watchHeartRecordVos.stream()
                    .mapToInt(WatchHeartRecordVo::getHeartRate).average().getAsDouble();
        }

        watchHeartDayVo.setHeartRateAvg(heartRateAvg);
        watchHeartDayVo.setHeartRateMax(heartRateMax);
        watchHeartDayVo.setHeartRateMin(heartRateMin);
        watchHeartDayVo.setHeartRecordVoList(watchHeartRecordVos);

        return watchHeartDayVo;
    }

    /**
     * 移除心跳数据
     *
     * @param deviceId the device id
     *
     * @return the int
     */
    @Override
    public Integer removeDmcWatchDataHeart(Long deviceId) {

        DmcWatchDataHeart dmcWatchDataHeart = new DmcWatchDataHeart();
        dmcWatchDataHeart.setDeviceId(deviceId);

        return dmcWatchDataHeartMapper.delete(dmcWatchDataHeart);
    }
}
